package cn.zwz.home.controller;

import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.SecurityUtil;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.home.entity.Student;
import cn.zwz.home.entity.StudentGrade;
import cn.zwz.home.service.IStudentGradeService;
import cn.hutool.core.util.StrUtil;
import cn.zwz.home.service.IStudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 郑为中
 */
@Slf4j
@RestController
@Api(tags = "学生成绩管理接口")
@RequestMapping("/zwz/studentGrade")
@Transactional
public class StudentGradeController {

    @Autowired
    private IStudentGradeService iStudentGradeService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条学生成绩")
    public Result<StudentGrade> get(@RequestParam String id){
        return new ResultUtil<StudentGrade>().setData(iStudentGradeService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部学生成绩个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iStudentGradeService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部学生成绩")
    public Result<List<StudentGrade>> getAll(){
        return new ResultUtil<List<StudentGrade>>().setData(iStudentGradeService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询学生成绩")
    public Result<IPage<StudentGrade>> getByPage(@ModelAttribute StudentGrade studentGrade ,@ModelAttribute PageVo page){
        QueryWrapper<StudentGrade> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(studentGrade.getStudentId())) {
            qw.eq("student_id",studentGrade.getStudentId());
        }
        if(!ZwzNullUtils.isNull(studentGrade.getTitle())) {
            qw.like("title",studentGrade.getTitle());
        }
        IPage<StudentGrade> data = iStudentGradeService.page(PageUtil.initMpPage(page),qw);
        for (StudentGrade grade : data.getRecords()) {
            if(!ZwzNullUtils.isNull(grade.getStudentId())) {
                Student stu = iStudentService.getById(grade.getStudentId());
                if(stu != null) {
                    grade.setStudentName(stu.getUserName());
                }
            }
        }
        return new ResultUtil<IPage<StudentGrade>>().setData(data);
    }

    @RequestMapping(value = "/getByMyPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询学生成绩")
    public Result<IPage<StudentGrade>> getByMyPage(@ModelAttribute StudentGrade studentGrade ,@ModelAttribute PageVo page){
        QueryWrapper<StudentGrade> qw = new QueryWrapper<>();
        qw.eq("student_id",securityUtil.getCurrUser().getStudentId());
        if(!ZwzNullUtils.isNull(studentGrade.getTitle())) {
            qw.like("title",studentGrade.getTitle());
        }
        IPage<StudentGrade> data = iStudentGradeService.page(PageUtil.initMpPage(page),qw);
        for (StudentGrade grade : data.getRecords()) {
            if(!ZwzNullUtils.isNull(grade.getStudentId())) {
                Student stu = iStudentService.getById(grade.getStudentId());
                if(stu != null) {
                    grade.setStudentName(stu.getUserName());
                }
            }
        }
        return new ResultUtil<IPage<StudentGrade>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改学生成绩")
    public Result<StudentGrade> saveOrUpdate(StudentGrade studentGrade){
        if(iStudentGradeService.saveOrUpdate(studentGrade)){
            return new ResultUtil<StudentGrade>().setData(studentGrade);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增学生成绩")
    public Result<StudentGrade> insert(StudentGrade studentGrade){
        if(ZwzNullUtils.isNull(studentGrade.getStudentId())) {
            return ResultUtil.error("学生不能为空");
        }
        Student stu = iStudentService.getById(studentGrade.getStudentId());
        if(stu == null) {
            return ResultUtil.error("学生不存在");
        }
        iStudentGradeService.saveOrUpdate(studentGrade);
        return new ResultUtil<StudentGrade>().setData(studentGrade);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑学生成绩")
    public Result<StudentGrade> update(StudentGrade studentGrade){
        if(ZwzNullUtils.isNull(studentGrade.getStudentId())) {
            return ResultUtil.error("学生不能为空");
        }
        Student stu = iStudentService.getById(studentGrade.getStudentId());
        if(stu == null) {
            return ResultUtil.error("学生不存在");
        }
        iStudentGradeService.saveOrUpdate(studentGrade);
        return new ResultUtil<StudentGrade>().setData(studentGrade);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除学生成绩")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iStudentGradeService.removeById(id);
        }
        return ResultUtil.success();
    }
}
